package com.zq.zqplayer.mvvm.recommend

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.zq.zqplayer.bean.RoomInfoBean
import com.zq.zqplayer.http.BaseObserver
import com.zq.zqplayer.http.request.LiveRequest
import com.zq.zqplayer.http.response.BaseResponse
import com.zq.zqplayer.http.service.VideoService
import dagger.hilt.android.lifecycle.HiltViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

@HiltViewModel
class RecommendViewModel : ViewModel{

    @Inject
    constructor() : super(){
    }

    @Inject
    lateinit var videoService: VideoService

    val toastMsg = MutableLiveData<String>()
    val isLoading = MutableLiveData<Boolean>()
    val onRefreshItems = MutableLiveData<ArrayList<RoomInfoBean>>()
    val onLoadMoreItems = MutableLiveData<ArrayList<RoomInfoBean>>()


    var currentPage:Int = 0;
    fun getZqVideoList(isRefresh:Boolean = true) {

        if(isRefresh){
            currentPage = 0;
        }

        val request = LiveRequest()
        request.offset = "${currentPage * 20}"
        request.limit = "20"

        videoService
            .getLiveList(HashMap(), request)
            .subscribeOn(Schedulers.io())
            .doOnSubscribe {
                isLoading.value = true
            }
            .subscribeOn(AndroidSchedulers.mainThread())//控制doOnSubscribe 所在线程
            .observeOn(AndroidSchedulers.mainThread())
            .doFinally {
                isLoading.value = false
            }
            .subscribe(object : BaseObserver<BaseResponse<ArrayList<RoomInfoBean>>>() {
                override fun onSuccessful(t: BaseResponse<ArrayList<RoomInfoBean>>?) {
                    if(t?.data != null){
                        currentPage++
                        if(isRefresh){
                            onRefreshItems.value = t.data
                        }else{
                            onLoadMoreItems.value = t.data
                        }
                    }
                }

                override fun onFailed(msg: String?) {
                    toastMsg.value = msg?:""
                }

            })
    }

}